/*
 * start.S: blob start code
 *
 * Copyright (C) 1999 2000 2001 Erik Mouw (J.A.K.Mouw@its.tudelft.nl) and
 *                     Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

.ident "$Id: start.S,v 1.6 2001/11/15 11:56:20 seletz Exp $"

#ifdef HAVE_CONFIG_H
# include <blob/config.h>
#endif

#include <blob/arch.h>

.text

/* Jump vector table as in table 3.1 in [1] */
.globl _start
_start:	b	reset
	b	undefined_instruction
	b	software_interrupt
	b	prefetch_abort
	b	data_abort
	b	not_used
	b	irq
	b	fiq


BLOB_START: .word BLOB_ABS_BASE_ADDR

/* the actual reset code */
reset:
	/* set CPU to SVC32 mode */
	mrs	r0, cpsr
	bic	r0, r0, #0x1f
	orr	r0, r0, #0x13
	msr	cpsr, r0

	/* setup memory */
	bl	memsetup

	/* int LED */
	bl	ledinit
	
	/* check the first 1MB of BLOB_START in increments of 4k */
	mov	r7, #0x1000
	mov	r6, r7, lsl #8	/* 4k << 2^8 = 1MB */
	ldr	r5, BLOB_START

mem_test_loop:
	mov	r0, r5
	bl	testram
	teq	r0, #1
	beq	badram

	add	r5, r5, r7
	subs	r6, r6, r7
	bne	mem_test_loop


relocate:
	adr	r0, _start

	/* relocate the second stage loader */
	add	r2, r0, #(64 * 1024)	/* blob maximum size is 64kB */
	add	r0, r0, #0x1000		/* skip first 4k bytes */
	ldr	r1, BLOB_START

	/* r0 = source address
	 * r1 = target address
	 * r2 = source end address
	 */
copy_loop:
	ldmia	r0!, {r3-r10}
	stmia	r1!, {r3-r10}
	cmp	r0, r2
	ble	copy_loop


	/* blob is copied to ram, so jump to it */
	ldr	r0, BLOB_START
	mov	pc, r0


badram:
	mov	r6, #2
	b	endless_blink

	

endless_blink:
	bl	wait
	mov	r0, r6
	bl	led_blink
	b	endless_blink
		
wait:
	/* busy wait loop*/
	mov	r5, #0x1000000
wait0:
	subs	r5, r5, #1
	bne	wait0
	mov	pc, lr


	
	
undefined_instruction:
	mov	r6, #3
	b	endless_blink




software_interrupt:
	/* NOTE: This is NOT an error! If you think that blob should return
	 * from software interrupts, you're plain WRONG. The source of the
	 * problem is in the kernel: you should *disable* CONFIG_ANGELBOOT
	 * simply because blob is not angel. -- Erik
	 */
	mov	r6, #4
	b	endless_blink





prefetch_abort:
	mov	r6, #5
	b	endless_blink




data_abort:
	mov	r6, #6
	b	endless_blink




not_used:
	/* we *should* never reach this */
	mov	r6, #7
	b	endless_blink




irq:
	mov	r6, #8
	b	endless_blink




fiq:
	mov	r6, #9
	b	endless_blink

